package com.txhy.certificatechain.ui.activity.user;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ninetripods.aopermission.permissionlib.annotation.NeedPermission;
import com.orhanobut.logger.Logger;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.presenter.user.LoginRegistPresenter;
import com.txhy.certificatechain.ui.activity.base.BaseActivity;
import com.txhy.certificatechain.ui.activity.main.MainActivity;
import com.txhy.certificatechain.ui.view.user.FingerView;
import com.txhy.certificatechain.ui.view.user.LoginView;
import com.txhy.certificatechain.utils.CommUtils;
import com.txhy.certificatechain.utils.CountDownTimerUtil;
import com.txhy.certificatechain.utils.FingerCheckUtils;
import com.txhy.certificatechain.utils.PermissionUtil;
import com.txhy.certificatechain.utils.PrefUtil;
import com.txhy.certificatechain.utils.ToastUtil;
import com.txhy.certificatechain.utils.UIUtils;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

import static com.txhy.certificatechain.application.App.exit;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: LoginActivity.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 登录Activity
 * 创建时间: 2018/9/19 17:40
 * 更新时间：2018/9/19 17:40
 * -----------------------------------------------------------------
 * 2018/9/19 17:40 : 创建 LoginActivity.java (侯建军);
 * -----------------------------------------------------------------
 */
public class LoginActivity extends BaseActivity<LoginView, LoginRegistPresenter>
        implements LoginView, FingerView, EasyPermissions.PermissionCallbacks {

    @BindView(R.id.im_close)
    ImageView im_close;

    @BindView(R.id.ed_phone)
    EditText ed_phone;
    @BindView(R.id.ed_code)
    EditText ed_code;
    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.tv_sendCode)
    TextView tv_sendCode;
    @BindView(R.id.img_logo)
    ImageView img_logo;

    FingerCheckUtils fingerCheckUtils;

    @Override
    protected LoginRegistPresenter createPresenter() {
        return new LoginRegistPresenter();
    }

    @Override
    protected int provideContentViewId() {
        //设置为无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置为全屏模式
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        boolean bool = PermissionUtil.checkWritePermission(this);
        if(bool){
           Logger.e("有定权限");
        }else{
            Logger.e("无定权限");
        }
        tv_sendCode.setClickable(false);         //设置不可点击
        fingerCheckUtils = new FingerCheckUtils(LoginActivity.this, LoginActivity.this);
    }

    @Override
    public void initListener() {
        super.initListener();
        ed_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int num = (ed_phone.getText() + "").length();
                if (num == 11) {
                    tv_sendCode.setBackground(UIUtils.getResource().getDrawable(R.drawable.editbg_select));
                    tv_sendCode.setTextColor(UIUtils.getResource().getColor(R.color.ed584c));
                    tv_sendCode.setClickable(true); //
                } else {
                    tv_sendCode.setBackground(UIUtils.getResource().getDrawable(R.drawable.edit_bg));
                    tv_sendCode.setTextColor(UIUtils.getResource().getColor(R.color.textColor));
                    tv_sendCode.setClickable(false); //设置不可点击
                }
            }
        });
    }

    @OnClick({R.id.im_close, R.id.tv_login, R.id.tv_sendCode, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //关闭
            case R.id.im_close:
                finish();
                break;
            //登录
            case R.id.tv_login:
                String phone = ed_phone.getText().toString().trim();
                String code = ed_code.getText().toString().trim();
                if (phone.length() != 11) {
                    ToastUtil.showShort(LoginActivity.this, UIUtils.getString(R.string.ninshuru));
                } else if (TextUtils.isEmpty(code)) {
                    ToastUtil.showShort(LoginActivity.this, UIUtils.getString(R.string.liangcishuyan));
                } else {
                    Logger.d("登录测试");
                    mPresenter.toLogin(phone, CommUtils.getIMEI(LoginActivity.this), code);
                }
                break;
            //获取验证码
            case R.id.tv_sendCode:
                Logger.e(CommUtils.getIMEI(LoginActivity.this));
                //发送短信验证
                mPresenter.SendCode(ed_phone.getText().toString().trim(), CommUtils.getIMEI(LoginActivity.this));
                img_logo.setVisibility(View.GONE);
                break;
            //立即注册
            case R.id.tv_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
//                ToastUtil.showShort(LoginActivity.this, "注册");
                finish();
                break;
        }
    }

    @Override
    public void showProgress(String tipString) {
        showWaitingDialog(tipString);
    }

    @Override
    public void hideProgress() {
        hideWaitingDialog();
    }

    @Override
    public void loginSuccess(String user) {
        switch (PrefUtil.getLoadType(LoginActivity.this)) {
            case 0:
                //显示指纹对话框
                fingerCheckUtils.showFingerDialog();
                break;
            case 1:
                //电话
                startActicity(ed_phone.getText().toString(), "1");
                break;
            case 2:
                //显示指纹对话框
                fingerCheckUtils.showFingerDialog();
                break;
            case 3:
                break;
        }
    }

    @Override
    public void loginFail(String msg) {
        ToastUtil.showShort(LoginActivity.this, msg);
    }

    @Override
    public void sendCodeSuccess(String msg) {
        CountDownTimerUtil mCountDownTimerUtils = new CountDownTimerUtil(tv_sendCode, 60000, 1000);
        mCountDownTimerUtils.start();
        ToastUtil.showLong(LoginActivity.this, "发送验证码成功");
    }

    @Override
    public void sendCodeFail(String msg) {
        ToastUtil.showLong(LoginActivity.this, msg);
    }

    @Override
    public void fingerSuccess(String msg) {
        mPresenter.toEnd(ed_phone.getText().toString(), msg);
    }

    @Override
    public void fingerFail(String msg) {
        ToastUtil.showLong(LoginActivity.this, msg);
    }

    @Override
    public void endSuccessSuccess(String msg) {
        PrefUtil.setLoginBoolean(LoginActivity.this, true);
        PrefUtil.setUserString(LoginActivity.this, msg);
        exit();
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void endFail(String msg) {
        ToastUtil.showLong(LoginActivity.this, msg);
    }

    /**
     * 获取指纹结果
     *
     * @param result
     */
    @Override
    public void getFingerResult(int result) {
        switch (result) {
            case 1:
                fingerCheckUtils.hideFingerDialog();
                switch (PrefUtil.getLoadType(LoginActivity.this)) {
                    case 0://指纹
                        mPresenter.toFingerLogin(ed_phone.getText().toString(), CommUtils.getIMEI(LoginActivity.this), "0", "");
                        break;
                    case 2://指纹人脸
                        startActicity(ed_phone.getText().toString(), "2");
                        break;
                }
                break;
        }
    }

    @NeedPermission(value = {Manifest.permission.CAMERA}, requestCode = 0)
    private void startActicity(String phone, String type) {
//        Intent intent=new Intent(LoginActivity.this, FaceDetectExpActivity.class);
//        intent.putExtra("phone",phone);
//        intent.putExtra("type",type);
//        intent.putExtra("activity",MainActivity.class);
//        intent.putExtra("gongneng","0");//0是登录
//        startActivity(intent);
        finish();
    }

    /**
     * 授权
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        ToastUtil.showShort(LoginActivity.this, "用户授权成功");
    }

    /**
     * 拒绝
     *
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        ToastUtil.showShort(LoginActivity.this, "用户授权失败");
    }
}
